package message;

import entity.ChessUser;
import model.RoomInfo;
import socket.ChessServer;

import java.util.List;

/**
 * 快速游戏报文.
 *
 * @author 张紘溢
 */
public class QuickEnterMessage extends BaseMessage {
    private ChessUser chessUser;

    public QuickEnterMessage(ChessUser chessUser) {
        this.chessUser = ChessServer.getInstance().getChessUserService().findUser(chessUser.getName());
    }

    @Override
    public void sendToServer() {
        List<RoomInfo> roomInfoList = ChessServer.getInstance().getRooms();
        RoomInfo resultRoom = null;
        //循环找出最优房间,优先寻找一人房间,其次为无人房间
        for (RoomInfo roomInfo : roomInfoList) {
            if (roomInfo.getStatus() == RoomInfo.WAIT) {
                resultRoom = roomInfo;
                break;
            } else if (roomInfo.getStatus() == RoomInfo.NONE) {
                resultRoom = roomInfo;
            }
        }
        if (resultRoom == null) {
            System.out.println("没空房");
        } else {
            resultRoom.setStatus(resultRoom.getStatus() + 1);//更新房间状态

            if (resultRoom.getLeftPlayer() == null) {
                resultRoom.setLeftPlayer(chessUser);
                if (resultRoom.getRightPlayer() != null) {
                    resultRoom.setRightPlayer(ChessServer.getInstance().getChessUserService().findUser(resultRoom.getRightPlayer().getName()));
                }
                //更新自己的页面
                ServerEnterRoomMessage serverEnterRoomMessage = new ServerEnterRoomMessage(resultRoom.getRid(), true, chessUser);
                ChessServer.getInstance().sendMsgToClient(serverEnterRoomMessage, this.sender.getClient());
                //更新房间列表
                ServerRoomListMessage serverRoomListMessage = new ServerRoomListMessage(ChessServer.getInstance().getRooms());
                ChessServer.getInstance().sendMsgToAll(serverRoomListMessage);
                //设置对手
                ServerRoomPlayerMessage serverRoomPlayerMessage = new ServerRoomPlayerMessage(resultRoom);
                ChessServer.getInstance().sendMsgToAll(serverRoomPlayerMessage);
            } else {
                resultRoom.setRightPlayer(chessUser);
                if (resultRoom.getLeftPlayer() != null) {
                    resultRoom.setLeftPlayer(ChessServer.getInstance().getChessUserService().findUser(resultRoom.getLeftPlayer().getName()));
                }
                ServerEnterRoomMessage serverEnterRoomMessage = new ServerEnterRoomMessage(resultRoom.getRid(), false, chessUser);
                ChessServer.getInstance().sendMsgToClient(serverEnterRoomMessage, this.sender.getClient());
                ServerRoomListMessage serverRoomListMessage = new ServerRoomListMessage(ChessServer.getInstance().getRooms());
                ChessServer.getInstance().sendMsgToAll(serverRoomListMessage);
                ServerRoomPlayerMessage serverRoomPlayerMessage = new ServerRoomPlayerMessage(resultRoom);
                ChessServer.getInstance().sendMsgToAll(serverRoomPlayerMessage);
            }
        }
    }

    @Override
    public void selfExecute() {

    }
}
